package com.example.bbs.controller;


import com.example.bbs.entity.Disease;
import com.example.bbs.entity.DiseaseTag;
import com.example.bbs.entity.Selfchecking;
import com.example.bbs.service.DiseaseService;
import com.example.bbs.service.DiseaseTagService;
import com.example.bbs.util.SelfcheckingResove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/disease")
public class DiseaseController {
    @Autowired
    DiseaseService diseaseService;
    @Autowired
    private DiseaseTagService diseaseTagService;

    @RequestMapping({"/", "/index"})
    public String defaultIndex(Model model) {
        List<Disease> diseases = diseaseService.selectParts();
        List<DiseaseTag> diseaseTags = diseaseTagService.selectAllDisease();
        model.addAttribute("diseaseTags", diseaseTags);
        model.addAttribute("selecttagid", 0);
        model.addAttribute("diseases", diseases);
        return "disease_list";
    }

    //获取 id title description view
    @RequestMapping("/parts_by_id/{id}")
    public String selectPartsById(@PathVariable("id") String id,Model model) {
        if (id == null||"0".equals(id)) {
            return defaultIndex(model);
        }
        Disease disease = diseaseService.selectPartsById(Integer.parseInt(id));
        List<DiseaseTag> diseaseTags = diseaseTagService.selectAllDisease();
        model.addAttribute("diseaseTags", diseaseTags);
        model.addAttribute("selecttagid", id);
        model.addAttribute("diseases", disease);
        return "disease_list";
    }

    //查看某一篇
    @RequestMapping("/select_by_id/{id}")
    public String selectDiseaseById(@PathVariable("id") String id, Model model) {
        if (id == null) {
            model.addAttribute("disease", null);
        } else {
            Disease disease = diseaseService.selectDiseaseById(Integer.parseInt(id));
            model.addAttribute("disease", disease);
        }
        return "disease_detail";
    }
}
